package com.ms.services.commons.schema;

import com.ms.exception.DpwcBusinessException;
import com.ms.exception.DpwcSystemException;
import org.springframework.util.StringUtils;

/**
 * User: gmc
 * Date: 04/02/14
 */
public class WsUtil {

    public static void validateResultInfo(ResultInfo resultInfo) {
        validateResultInfo(resultInfo, null);
    }

    public static void validateResultInfo(ResultInfo resultInfo, String headerMsg) {
        headerMsg = getHeaderMsg(headerMsg);
        if ("-1".equals(resultInfo.getResultCode())) {
            throw new DpwcSystemException(headerMsg + "Error:" + resultInfo.getErrorMsg(), resultInfo.getResultMsg());
        }
        if (!"0".equals(resultInfo.getResultCode())) {
            if(StringUtils.hasText(resultInfo.getResultMsg())){
                Throwable base = new DpwcSystemException(resultInfo.getResultMsg());
                throw new DpwcBusinessException(headerMsg + resultInfo.getErrorMsg(),base);
            }
            throw new DpwcBusinessException(headerMsg + resultInfo.getErrorMsg());
        }
    }

    private static String getHeaderMsg(String headerMsg) {
        if (headerMsg == null) {
            return "";
        }
        return headerMsg + ": ";
    }
}
